package com.metacube.questionbank.dao;

import java.util.List;
import java.util.ArrayList;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.metacube.questionbank.dto.UserDTO;
import com.metacube.questionbank.jdbc.UserRowMapper;

@Repository
public class UserDAO {

	@Autowired
	DataSource dataSource;

	public void insertUserData(UserDTO userDTO) {

		String query = "insert into user(NAME, EMAIL, PASSWORD)"
				+ " values(?,?,?)";
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		System.out.println("name : "+userDTO.getName());
		jdbcTemplate.update(
				query,
				new Object[] { userDTO.getName(), userDTO.geteMail(),
						userDTO.getPassword() });
	}

	public List<UserDTO> checkLoginInfoFromDB(UserDTO userDTO) {

		List<UserDTO> userList = new ArrayList<UserDTO>();

		String query = "select * from user where EMAIL = ? and PASSWORD = ?";
		
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

		userList = jdbcTemplate.query(query, new Object[]{userDTO.geteMail(),userDTO.getPassword()},new UserRowMapper());
		
		return userList;
	}

	public int updateProfile(UserDTO userDTO) {
		
		String query = "update user set NAME = ?, POSITION = ?, LOCATION = ?, ORGANISATION = ? where USERID = ?";
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		
		return jdbcTemplate.update(
				query,
				new Object[] { userDTO.getName(), userDTO.getPosition(),
						userDTO.getLocation(), userDTO.getOrganisation(), userDTO.getId()});
	}
	
	public int changePassword(String password, String currentPassword, int userId) {
		
		String query = "update user set password = ? where USERID = ? and PASSWORD = ?";
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		
		return jdbcTemplate.update(
				query,
				new Object[] { password, userId,
						currentPassword });

	}
	
	public List<UserDTO> retrieveDataFromDB() {

		List<UserDTO> userList = new ArrayList<UserDTO>();

		String query = "select * from user";
		
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

		userList = jdbcTemplate.query(query, new UserRowMapper());
		
		return userList;
	}

	public int getUserId(String eMail) {
		
		List<UserDTO> userList = new ArrayList<UserDTO>();
		
		String query = "select * from user where EMAIL = ?";
		
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		
		userList = jdbcTemplate.query(query, new Object[]{eMail},new UserRowMapper());

		return userList.get(0).getId();
	}

	public List<UserDTO> retrieveProfileFromDB(int userId) {

		List<UserDTO> userList = new ArrayList<UserDTO>();

		String query = "select * from user where userId = ?";

		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

		userList = jdbcTemplate.query(query, new Object[] {userId},
				new UserRowMapper());

		return userList;
	}

}
